﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Nova_DAL;
using System.Data.Common;

public partial class Views_CargoWeight : System.Web.UI.Page
{
    DataSet ds = null;
    static int iCount = 0;
    int _SLine = 0;
    int _SLcol = 0;
    int _ELine = 0;
    int _ELcol = 0;
    int iLine = 0;
    int iLcol = 0;
    string strShowColor = "";
    DbCommand cmd = null;
    int iMessage = 0;
    string strSQL = "";
    Nova_DAL.DbHelper access = new DbHelper();
    Nova_BLL.TSysoption Nova_BLL_TSysoption = new Nova_BLL.TSysoption();
    Nova_BLL.TLaneDefine Nova_BLL_TLaneDefine = new Nova_BLL.TLaneDefine();
    Nova_BLL.TBase_Position Nova_BLL_TBase_Position = new Nova_BLL.TBase_Position();
    Nova_MOD.TBase_Position Nova_MOD_TBase_Position = new Nova_MOD.TBase_Position();

    protected void Page_Load(object sender, EventArgs e)
    {
        gvType_Weight_Bind();
        if (!IsPostBack)
        {
            ddlNo_Lane_ABC_Bind();
            ddlType_Weight_Bind();

            gv_Weight_Bind();
        }
    }

    /// <summary>
    /// 绑定gv显示颜色
    /// </summary>
    protected void gvType_Weight_Bind()
    {
        strSQL = "SELECT * FROM TWeightDefineMain ORDER BY PID";
        cmd = access.GetSqlStringCommond(strSQL);
        gvType_Weight.DataSource = access.ExecuteDataSet(cmd);
        gvType_Weight.DataBind();



    }

    protected void gvType_Weight_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label labShowColor = (Label)e.Row.Cells[2].FindControl("labShowColor");
            if (labShowColor.ToolTip.ToString() == "" || labShowColor.ToolTip.ToString() == "0")
            {
                strShowColor = "#cecece";
            }
            else
            {
                strShowColor = "#" + labShowColor.ToolTip.ToString();
            }
            labShowColor.BackColor = System.Drawing.Color.FromName(strShowColor);
        }
    }

    /// <summary>
    /// 绑定下拉列表
    /// </summary>
    protected void ddlType_Weight_Bind()
    {
        strSQL = "SELECT * FROM TWeightDefineMain ORDER BY PID";
        cmd = access.GetSqlStringCommond(strSQL);
        ddlType_Weight.DataSource = access.ExecuteDataSet(cmd);
        ddlType_Weight.DataBind();
        ddlType_Weight.Items.Insert(0, new ListItem("请选择", "0"));
    }







    /// <summary>
    /// 绑定gv
    /// </summary>
    public void gv_Weight_Bind()
    {
        ds = Nova_BLL_TSysoption.TSysoptionList_Size("Sp_TypeOfWeightPosition", ddlNo_Lane.SelectedValue);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            gv_Weight.DataSource = ds;
            gv_Weight.DataBind();
            iCount = ds.Tables[0].Columns.Count;
        }
        else
        {
            iCount = 0;
        }
    }

    protected void gv_Weight_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Cells[2].Text = "?";
            int rid = e.Row.RowIndex + 1;
            string strColor = "";
            for (int i = 2; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;

                //strColor = Nova_BLL_TSysoption.getColor(e.Row.Cells[i].ToolTip.ToString());

                strSQL = "SELECT * FROM TWeightDefineMain WHERE Pos_Weight = '" + e.Row.Cells[i].ToolTip.ToString() + "'";
                cmd = access.GetSqlStringCommond(strSQL);
                ds = access.ExecuteDataSet(cmd);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    strColor = "#" + ds.Tables[0].Rows[0]["ShowColor"].ToString();
                }
                else
                {
                    strColor = "";
                }
                if (strColor != null && strColor != "")
                {
                    e.Row.Cells[i].BackColor = System.Drawing.Color.FromName(strColor);
                }
                e.Row.Cells[i].Text = "";


                //巷道
                Nova_MOD_TBase_Position.No_Lane = Convert.ToInt32(ddlNo_Lane.SelectedValue);
                //排
                Nova_MOD_TBase_Position.No_Line = Convert.ToInt32(e.Row.Cells[0].Text.ToString());
                //列
                Nova_MOD_TBase_Position.No_Col = i-1;  //列从1开始
                //Nova_MOD_TBase_Position.No_Col = i;  //列从2开始
                //层
                Nova_MOD_TBase_Position.No_Layer = Convert.ToInt32(e.Row.Cells[1].Text.ToString());

                e.Row.Cells[i].Attributes["onclick"] = "ShowEdit(" + Nova_MOD_TBase_Position.No_Lane + "," + Nova_MOD_TBase_Position.No_Line + "," + Nova_MOD_TBase_Position.No_Col + "," + Nova_MOD_TBase_Position.No_Layer + "," + rid + ");";
            }
        }
    }
    /// <summary>
    /// 巷道下拉列表绑定
    /// </summary>
    protected void ddlNo_Lane_ABC_Bind()
    {
        DataSet dds = Nova_BLL_TLaneDefine.DataSetTLaneDefine();
        if (dds != null && dds.Tables.Count > 0 && dds.Tables[0].Rows.Count > 0)
        {
            ddlNo_Lane.DataSource = dds;
            ddlNo_Lane.DataValueField = "No_Lane";
            ddlNo_Lane.DataTextField = "No_Lane_Text";
            ddlNo_Lane.DataBind();
        }
    }

    /// <summary>
    /// 巷道改变时gv发生改变
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlNo_Lane_SelectedIndexChanged(object sender, EventArgs e)
    {
        gv_Weight_Bind();
    }
    /// <summary>
    /// 重新设定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReset_Weight_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (ddlType_Weight.SelectedValue == "0")
            {
                lblKindMessage.Text = "操作失败!请选择货位重量类型!";
            }
            else
            {
                if (txtSLine.Text != "" || txtSLcol.Text != "")
                {
                    _SLine = Convert.ToInt32(txtSLine.Text.ToString());
                    _SLcol = Convert.ToInt32(txtSLcol.Text.ToString());

                    if (txtELine.Text != "" || txtELcol.Text != "")
                    {
                        _ELine = Convert.ToInt32(txtELine.Text.ToString());
                        _ELcol = Convert.ToInt32(txtELcol.Text.ToString());
                        iLine = 0;
                        iLcol = 0;

                        if (_SLine > _ELine)
                        {
                            iLine = _SLine;
                            _SLine = _ELine;
                            _ELine = iLine;
                        }
                        if (_SLcol > _ELcol)
                        {
                            iLcol = _SLcol;
                            _SLcol = _ELcol;
                            _ELcol = iLcol;
                        }

                        for (int i = 0; i <= gv_Weight.Rows.Count; i++)
                        {
                            if (i >= Convert.ToInt32(_SLine) && i <= Convert.ToInt32(_ELine))
                            {
                                for (int j = 0; j < iCount; j++)
                                {
                                    if (j >= Convert.ToInt32(_SLcol) && j <= Convert.ToInt32(_ELcol))
                                    {
                                        //巷道iPid、排iNo_Line、列iNo_Col、层iNo_Layer
                                        Nova_MOD_TBase_Position.No_Lane = Convert.ToInt32(ddlNo_Lane.SelectedValue);
                                        Nova_MOD_TBase_Position.No_Line = Convert.ToInt32(gv_Weight.Rows[i - 1].Cells[0].Text.ToString());
                                        Nova_MOD_TBase_Position.No_Col = j;
                                        Nova_MOD_TBase_Position.No_Layer = Convert.ToInt32(gv_Weight.Rows[i - 1].Cells[1].Text.ToString());

                                        iMessage = Nova_BLL_TBase_Position.UpdateTBase_Position(Nova_MOD_TBase_Position, "TypeOfWeight", ddlType_Weight.SelectedValue);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        //巷道iPid、排iNo_Line、列iNo_Col、层iNo_Layer
                        Nova_MOD_TBase_Position.No_Lane = Convert.ToInt32(ddlNo_Lane.SelectedValue);
                        Nova_MOD_TBase_Position.No_Line = Convert.ToInt32(gv_Weight.Rows[Convert.ToInt32(_SLine) - 1].Cells[0].Text.ToString());
                        Nova_MOD_TBase_Position.No_Col = Convert.ToInt32(_SLcol);
                        Nova_MOD_TBase_Position.No_Layer = Convert.ToInt32(gv_Weight.Rows[Convert.ToInt32(_SLine) - 1].Cells[1].Text.ToString());

                        iMessage = Nova_BLL_TBase_Position.UpdateTBase_Position(Nova_MOD_TBase_Position, "TypeOfWeight", ddlType_Weight.SelectedValue);
                    }
                    if (iMessage == 1)
                    {
                        lblKindMessage.Text = "操作成功!";
                        ibtnReset_Click(sender, e);
                    }
                    else
                    {
                        lblKindMessage.Text = "操作失败!";
                    }
                }
            }
        }
        catch(Exception ex)
        {
            lblKindMessage.Text = "异常跳出!";
        }
    }

    /// <summary>
    /// 删除限制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDel_Weight_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (txtSLine.Text != "" || txtSLcol.Text != "")
            {
                _SLine = Convert.ToInt32(txtSLine.Text.ToString());
                _SLcol = Convert.ToInt32(txtSLcol.Text.ToString());

                if (txtELine.Text != "" || txtELcol.Text != "")
                {
                    _ELine = Convert.ToInt32(txtELine.Text.ToString());
                    _ELcol = Convert.ToInt32(txtELcol.Text.ToString());
                    iLine = 0;
                    iLcol = 0;

                    if (_SLine > _ELine)
                    {
                        iLine = _SLine;
                        _SLine = _ELine;
                        _ELine = iLine;
                    }
                    if (_SLcol > _ELcol)
                    {
                        iLcol = _SLcol;
                        _SLcol = _ELcol;
                        _ELcol = iLcol;
                    }

                    for (int i = 0; i <= gv_Weight.Rows.Count; i++)
                    {
                        if (i >= Convert.ToInt32(_SLine) && i <= Convert.ToInt32(_ELine))
                        {
                            for (int j = 0; j < iCount; j++)
                            {
                                if (j >= Convert.ToInt32(_SLcol) && j <= Convert.ToInt32(_ELcol))
                                {
                                    //巷道iPid、排iNo_Line、列iNo_Col、层iNo_Layer
                                    Nova_MOD_TBase_Position.No_Lane = Convert.ToInt32(ddlNo_Lane.SelectedValue);
                                    Nova_MOD_TBase_Position.No_Line = Convert.ToInt32(gv_Weight.Rows[i - 1].Cells[0].Text.ToString());
                                    Nova_MOD_TBase_Position.No_Col = j;
                                    Nova_MOD_TBase_Position.No_Layer = Convert.ToInt32(gv_Weight.Rows[i - 1].Cells[1].Text.ToString());

                                    iMessage = Nova_BLL_TBase_Position.UpdateTBase_Position(Nova_MOD_TBase_Position, "TypeOfWeight", "0");
                                }
                            }
                        }
                    }
                }
                else
                {
                    //巷道iPid、排iNo_Line、列iNo_Col、层iNo_Layer
                    Nova_MOD_TBase_Position.No_Lane = Convert.ToInt32(ddlNo_Lane.SelectedValue);
                    Nova_MOD_TBase_Position.No_Line = Convert.ToInt32(gv_Weight.Rows[Convert.ToInt32(_SLine) - 1].Cells[0].Text.ToString());
                    Nova_MOD_TBase_Position.No_Col = Convert.ToInt32(_SLcol);
                    Nova_MOD_TBase_Position.No_Layer = Convert.ToInt32(gv_Weight.Rows[Convert.ToInt32(_SLine) - 1].Cells[1].Text.ToString());

                    iMessage = Nova_BLL_TBase_Position.UpdateTBase_Position(Nova_MOD_TBase_Position, "TypeOfWeight", "0");
                }

                if (iMessage == 1)
                {
                    lblKindMessage.Text = "操作成功!";
                    gv_Weight_Bind();
                }
                else
                {
                    lblKindMessage.Text = "操作失败!";
                }
            }
        }
        catch (Exception ex)
        {
            lblKindMessage.Text = "异常失败!";
        }
    }

    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtSLcol.Text = "";
        txtSLine.Text = "";
        txtELcol.Text = "";
        txtELine.Text = "";
        lblKindMessage.Text = "";
        gv_Weight_Bind();
    }
    protected void ibtnSelect_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (txtSLine.Text != "" || txtSLcol.Text != "")
            {
                _SLine = Convert.ToInt32(txtSLine.Text.ToString());
                _SLcol = Convert.ToInt32(txtSLcol.Text.ToString());

                if (txtELine.Text != "" || txtELcol.Text != "")
                {
                    _ELine = Convert.ToInt32(txtELine.Text.ToString());
                    _ELcol = Convert.ToInt32(txtELcol.Text.ToString());
                    iLine = 0;
                    iLcol = 0;

                    if (_SLine > _ELine)
                    {
                        iLine = _SLine;
                        _SLine = _ELine;
                        _ELine = iLine;
                    }
                    if (_SLcol > _ELcol)
                    {
                        iLcol = _SLcol;
                        _SLcol = _ELcol;
                        _ELcol = iLcol;
                    }

                    for (int i = 0; i <= gv_Weight.Rows.Count; i++)
                    {
                        if (i >= Convert.ToInt32(_SLine) && i <= Convert.ToInt32(_ELine))
                        {
                            for (int j = 0; j < iCount; j++)
                            {
                                if (j >= Convert.ToInt32(_SLcol) && j <= Convert.ToInt32(_ELcol))
                                {
                                    gv_Weight.Rows[i - 1].Cells[j + 1].BackColor = System.Drawing.Color.FromName("#ff0000");//改变背景色  从1列开始
                                    //gv_Weight.Rows[i - 1].Cells[j].BackColor = System.Drawing.Color.FromName("#ff0000");//改变背景色   从2列开始
                                }
                            }
                        }
                    }
                }
                else
                {
                    gv_Weight.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text) + 1].BackColor = System.Drawing.Color.FromName("#ff0000"); //改变背景色  从1列开始
                    //gv_Weight.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text)].BackColor = System.Drawing.Color.FromName("#ff0000");   //改变背景色   从2列开始
                }
            }
        }
        catch (Exception ex)
        {
            lblKindMessage.Text = "异常失败!";
        }
    }
    
}
